package com.mybatis.mapper;

import com.mybatis.model.UserInfo;
import lombok.Data;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
   // @ResultMap("BaseMap")
    @Select("select * from user_info")
    List<UserInfo> queryUserInfos();

    @Select("select * from user_info where id = #{id}")
    UserInfo queryUserInfo(Integer id);

    @Select("select * from user_info where username = '${username}'")
    UserInfo queryUserInfoByName(String username);


    @Select("select * from user_info where id = #{id} and username = #{username}")
    UserInfo queryUserInfoByIdAndName(Integer id, String username);

    @Select("select * from user_info where id = #{id} and username = #{username}")
    UserInfo queryUserInfoByIdAndName2(@Param("id1") Integer id,@Param("name1") String username);
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username, password,age,gender,phone) " +
            "values(#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username, password,age,gender,phone) " +
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}" )
    Integer delete(Integer id);

    @Update("update user_info set password =#{password} where id = #{id}")
    Integer update(UserInfo userInfo);

    //起别名
    @Select("select id,username,password,age,gender,phone," +
            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime from user_info" )
    List<UserInfo> queryUserInfos2();


    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from user_info" )
    List<UserInfo> queryUserInfos3();

    @Select("select * from user_info")
    List<UserInfo> queryUserInfos4();

    //排序
    @Select("select * from user_info order by id ${order}")
    List<UserInfo> queryUserInfoByOrder(String order);


    @Select("select * from user_info where username like CONCAT('%',#{name},'%')")
    List<UserInfo> queryUserInfoByLike(String name);

    @Insert("<script> insert into user_info (username, password,\n" +
            "        <if test='age!=null'>age,</if>\n" +
            "        gender," +
            "        phone" +
            "        )" +
            "        values (#{username}, #{password}," +
            "        <if test='age!=null'>#{age},</if>" +
            "        #{gender}," +
            "        #{phone}" +
            "        )</script>")
    Integer insertByCondition(UserInfo userInfo);


}
